from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time
import pywinauto


# 初始化浏览器驱动
driver = webdriver.Chrome(ChromeDriverManager().install())
# 浏览器最大化
driver.maximize_window()
# 打开网易见外工作台首页
driver.get("https://jianwai.youdao.com/")
time.sleep(3)

# 切换表单，切换进入内联框架
iframe_ele = driver.find_element(By.CSS_SELECTOR, "iframe[id]")  # 定位表单
driver.switch_to.frame(iframe_ele)  # 切换进入表单

# 输入邮箱
driver.find_element(By.CSS_SELECTOR, "input[name=email]").send_keys("974381044@qq.com")
time.sleep(2)

# 输入密码
driver.find_element(By.XPATH, "//input[@name='password']").send_keys("zytc@12310120")
time.sleep(2)

# 点击-登录
driver.find_element(By.ID, "dologin").click()
time.sleep(5)

# 点击-新建项目
driver.find_element(By.XPATH, "//div[@class='btn btn-add']").click()
time.sleep(2)
# 点击-图片翻译
driver.find_element(By.XPATH, "//div[@class='title' and text()='图片翻译']").click()
time.sleep(3)

# # input标签文件上传
# driver.find_element(By.CLASS_NAME, "file").send_keys(r"C:\TT\习知\测开代码\python-basic-warehouse-td2\selenium-下\1624958787(1).png")
# time.sleep(5)

# 非input标签使用pywinauto实现文件上传
driver.find_element(By.XPATH, "//input[@class='file']/..").click()
time.sleep(2)
app = pywinauto.Desktop()
win = app['打开']
win['Edit'].type_keys(r"C:\TT\习知\测开代码\python-basic-warehouse-td2\selenium-下\1624958787.png")
time.sleep(1)
win["Button"].click()
time.sleep(5)

driver.quit()